package main

import (
	"fmt"
)

func main() {
	var height = []int{3, 1, 2, 4}
	fmt.Println(maxArea(height))
}

func maxArea(height []int) int {
	max := 0
	i, j := 0, len(height)-1
	for i < j {
		tmp := cal(i, j, height[i], height[j])
		if height[i] > height[j] {
			j--
		} else {
			i++
		}

		if tmp > max {
			max = tmp
		}
	}
	return max
}

func cal(i, j, hi, hj int) int {
	sm := 0
	if hi > hj {
		sm = hj
	} else {
		sm = hi
	}
	return sm * (j - i)
}
